/**
 * <p>Title: PathUtil.java</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2012</p>
 * <p>Company: shundr</p>
 * @author Wei.Guo
 * @date 2015-4-29
 * @version 1.0
 */

package com.iot.ic.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * <p>
 * Title: PathUtil
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Company: shundr
 * </p>
 * 
 * @author Wei.Guo
 * @date 2015-4-29
 */
public class PathUtil {
	private static Logger logger = LoggerFactory.getLogger(PathUtil.class);
	
	public static final String NOTICE_IMG="noticeimg";//车辆驶出停车场照片存放路径
	
	public static final String ROM="course";//车辆驶出停车场照片存放路径
	

	/**
	 * 获取项目的相对路径下文件的绝对路径
	 * 
	 * @param parentDir
	 *            目标文件的父目录,例如说,工程的目录下,有lib与bin和conf目录,那么程序运行于lib or
	 *            bin,那么需要的配置文件却是conf里面,则需要找到该配置文件的绝对路径
	 * @param fileName
	 *            文件名
	 * @throws UnsupportedEncodingException
	 * @return一个绝对路径
	 */
	public static String getPath(String parentDir, String fileName) throws UnsupportedEncodingException {

		String path = null;
		String userdir = getRootPath();
		if (parentDir.trim().equals("")) {
			path = userdir + fileName;
		} else {
			path = userdir + parentDir + File.separator + fileName;
		}
		path = path.replace("/", File.separator);
		path = path.replace("\\", File.separator);
		return path;
	}

	/**
	 * 获取文件路劲
	 * @param fileDir  子路径
	 * @return
     */
	public static String getFilePath(String fileDir){
		String uploadPath =getUploadPath();
		String filePath=uploadPath+fileDir;
		filePath = filePath.replace("/", File.separator);
		filePath = filePath.replace("\\", File.separator);
		return filePath;
	}

	/**
	 * 获取根路径
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static String getRootPath() {
		// final String resourceFileName = "applicationContext.xml";
		String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
		try {
			path = URLDecoder.decode(path, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		logger.info(path);
		path = path.substring(0, path.indexOf("WEB-INF/classes"));
		logger.info(path);
		return path;
	}
	
	
	public static String getUrl(HttpServletRequest request, String fileName){
		return getBaseUrl(request)+"/upload"+fileName;
	}
	
	public static String getBaseUrl(HttpServletRequest request){
		String currentUrl=new String(request.getRequestURL());
		String pathUrl=request.getServletPath();
		String baseUrl=currentUrl.substring(0, currentUrl.indexOf(pathUrl));
		return baseUrl;
	}
	
	
	public static String getUploadPath(){
		String uploadPath = getRootPath() + "uploadPic/";
		uploadPath = uploadPath.substring(0, uploadPath.indexOf("zkhl"))+ "uploadPic/";
		return uploadPath;
	}

	/**
	 * <p>
	 * Title: getComtradePath
	 * </p>
	 * <p>
	 * Description: 获取comtrade文件下载路径
	 * </p>
	 * 
	 * @return comtrade文件下载路径
	 * @throws UnsupportedEncodingException
	 */
	public String getDataTypePath() throws UnsupportedEncodingException {
		return getRootPath() + "datatype/";
	}

	/**
	 * <p>
	 * Title: getComtradePath
	 * </p>
	 * <p>
	 * Description: 获取comtrade文件下载路径
	 * </p>
	 * 
	 * @return comtrade文件下载路径
	 * @throws UnsupportedEncodingException
	 */
	public String getComtradePath() throws UnsupportedEncodingException {
		return getRootPath() + "comtrade/";
	}

	/**
	 * <p>
	 * Title: getTempPath
	 * </p>
	 * <p>
	 * Description: 获取临时文件存放地址
	 * </p>
	 * 
	 * @return
	 */
	public String getTempPath() {
		return getWebRoot() + "temp/";
	}
	

	/**
	 * <p>
	 * Title: getWebRoot
	 * </p>
	 * <p>
	 * Description: 获取WebRoot路径
	 * </p>
	 * 
	 * @return WebRoot路径字符串
	 */
	public String getWebRoot() {
		String path = "";

		path = getRootPath();

		if (path.indexOf("WEB-INF") > 0) {
			path = path.substring(0, path.indexOf("WEB-INF"));
		} else {
			path = System.getProperty("user.dir");
			path = path + "/";
		}

		return path;
	}
}
